Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Why am I getting an "undefined" result from my MongoDBQuery in Node?

I've already stablished a DB with it's schema like this:
enter image description here

When I use my search function in Nodejs it works perfectly and returns my info like this:\

enter image description here

So, why is is than when I try to fectch my user from mongodb and get the parameter "examenes":
enter image description here

I get this undefined return?:
enter image description here

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think you should use the await keyword

try{
   var user = await model.findOne({ _id: req.params.id});
} catch (e) {
   // Handle Errors
};

Or

model.findOne({ _id: req.params.id}).then(result => {
  var user = result;
}).catch(e=> {
// Handle Errors
});
about 4 years ago · Juan Pablo Isaza Report

0

Have you added mergeParams in your router? This options adds params object to the request const router = express.Router({ mergeParams: true });

then /predict/:id results in req.params.id

By default this option is disabled in express

about 4 years ago · Juan Pablo Isaza Report

0

The problem is you are not using await so in the line console.log(user.examenes) the variable user is not the data returned by mongo; is another object without attribute examenes. And trying to access the value is undefined.

So you can use:

let user = await (usuario.findOne({_id: req.params.id}))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!